def prost(num):
    for i in range(2, int(num**0.5) +1):
        if num % i == 0:
            return False
    return True

def max_prost(num):
    if num < 1000 or num > 9999:
        return 0

    t4 = num // 1000
    t3 = (num % 1000) // 100
    t2 = (num%100) // 10
    t1 = num % 10
    maxim = 0
    for cur in (t1, t2, t3, t4, t4*10 +t3, t3*10 +t2,t2*10 +t1,t4*100 +t3*10+t2, t3*100 +t2*10+t1,num):
        if prost(cur) and cur>maxim:
            maxim= cur
    return maxim

num = int(input("Введите четырехзнаное натуральное число: "))
res = max_prost(num)
print(res)
